package cz.apnetis.fitweb.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/*
import com.google.appengine.api.oauth.OAuthRequestException;
import com.google.appengine.api.oauth.OAuthService;
import com.google.appengine.api.oauth.OAuthServiceFactory;
import com.google.appengine.api.oauth.OAuthServiceFailureException;
*/

/** Factory which create user from logged context */
public class UserFactory {

  private final static Logger LOGGER = LoggerFactory.getLogger(UserFactory.class);

  /** User for everyboday */
  public final static User GUEST = new UserImpl("GUEST", "GUEST", "GUEST");

  /** User which is currently logged if nobody sign up then return {@link #GUEST},
   * but never return null. */
  public final static User currentUser() {    
    com.google.appengine.api.oauth.OAuthService service = OAuthServiceFactory.getOAuthService();
/*
    OAuthService oService = new ServiceBuilder().provider(TwitterApi.class)
      .apiKey("http://fit4healt.appspot.com/").apiSecret("fit4health_test").build();
    Token requestToken = oService.getRequestToken();

    String yourToken = requestToken.getToken();
    LOGGER.debug("Your token: " + yourToken);

    Verifier verifier = new Verifier(yourToken);
    Token accessToken = oService.getAccessToken(requestToken, verifier);

    LOGGER.debug("Access token: " + accessToken.getToken());

    // try {
      LOGGER.info("Try to get user from OAuth");
      /*
     com.google.appengine.api.users.User gaeUser = oauth.getCurrentUser();
     */
      
     // UserService service = UserServiceFactory.getUserService();
    try {
      com.google.appengine.api.users.User gaeUser = service.getCurrentUser();
      if (gaeUser == null) {
        LOGGER.info("User is null.");
        return GUEST;
      }
      LOGGER.info("User id is: " + gaeUser.getUserId());
      return new UserImpl(gaeUser.getUserId(), gaeUser.getEmail(), gaeUser.getNickname());
    } catch (OAuthRequestException e) {
      LOGGER.warn("OAuthRequestExpecetion: " + e.toString(), e);
      return GUEST;
    }
  }

  private static class UserImpl implements User {
    private final String userId; @Override public String getEmail() { return userId; }
    private final String email; @Override public String getUserId() { return email; }
    private final String nickname; @Override public String getNickname() { return nickname; }

    public UserImpl(String userId, String email, String nickname) {
      this.userId = userId;
      this.email = email;
      this.nickname = nickname;
    }

    @Override
    public int hashCode() {
      if (userId == null) { return 31; }
      return 31 * userId.hashCode();
    }

    @Override
    public boolean equals(Object o) {
      if (o == null) { return false; }
      if (o.getClass() != getClass()) { return false; }
      User u = (User) o;
      if (getUserId() == null) {
        if (u.getUserId() == null) { return true; }
        return false;
      }
      return getUserId().equals(u.getUserId());
    }
  }
}
